<template src="./template.html"></template>

<style src="./style.css" scoped></style>
<script>
    import api from 'api/api';
    import modal from 'utils/modal';

    export default{
        data(){
            return {
                aid: null,
                bannerImg : null,
                bannerTitle: '',
                bannerDes: '',
                goodsList: [],
                start: 0,
                limit: 9,
                listCompleted: false,
                loading: false,
                listLoaded: false
            }
        },
        created(){
            let self = this;
            let config = weex.config;
            self.aid = config.aid;

            if(!self.aid) {
                modal.toast("活动ID错误");
                return false;
            }

            self.sendHttp(res=>{
                self.listLoaded = true;
                self.bannerImg = res.data.img;
                self.bannerTitle = res.data.title;
                self.bannerDes = res.data.desc;
                self.goodsList = res.data.list || [];

                //总数据量小于limit
                if(self.goodsList.length < self.limit) {
                    self.listCompleted = true;
                }
            });

        },
        methods: {
            sendHttp(succCallback,failCallback){
                let self = this;
                api.sendHttp('getActivitySuperApi', {aid: self.aid,start: self.start, limit: self.limit}).then(
                res =>{
                    if(typeof succCallback == "function") succCallback(res)
                }, res=>{
                    modal.toast(res.msg);
                    if(typeof failCallback == "function") failCallback(res)
                });
            },
            listFetchMore() {
                let self = this;
                if(self.listCompleted || self.loading) {
                    return false;
                }

                self.loading = true;
                let temp = self.start + self.limit;
                self.start = temp;

                self.sendHttp(res=>{
                    self.loading = false;
                    let data = res.data || {};
                    let _goodsList = data.list || [];
                    if(_goodsList.length < self.limit) self.listCompleted = true;
                    self.goodsList = self.goodsList.concat(_goodsList);
                }, res=>{
                    self.loading = false;
                });
            }
        }
    }
</script>
